﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RenameTracker
{
    public static class StringExtensions
    {
        /// <summary>
        /// "c:\foo\bar\baz\quux.txt".SplitLast('\') => { "c:\foo\bar\baz\", "quux.txt" }
        /// </summary>        
        public static List<string> SplitLast(this string str, char separator)
        {
            int index = str.LastIndexOf(separator);
            if (index < 0)
            {
                return null;
            }

            return new List<string> { str.Substring(0, index), str.Substring(index + 1) };
        }

        /// <summary>
        /// "c:\foo\bar\baz\quux.txt".SplitCumulative('\') => { "c:\", "c:\foo\", "c:\foo\bar\", "c:\foo\bar\baz\", "c:\foo\bar\baz\quux.txt" }
        /// note: trailing separators are trimmed
        /// </summary>        
        public static List<string> SplitCumulative(this string str, char separator)
        {
            str = str.TrimEnd(separator);

            var list = new List<string>();            
            for (int i = 0; i < str.Length - 1; ++i)
            {
                if (str[i] == separator)
                {
                    list.Add(str.Substring(0, i));
                }
            }
            list.Add(str);

            return list;
        }
    }
}
